<template>
  <!-- 教职工管理 -->
  <div class="mod-sys__params">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item prop="realName" label="姓名">
        <el-input v-model.trim="state.dataForm.realName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item prop="roleName" label="角色名称">
        <el-select v-model="state.dataForm.roleName" placeholder="角色名称" clearable filterable>
          <el-option v-for="(item,index) in roleList" :key="index" :label="item.name" :value="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="state.deleteHandle()">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="headUrl" label="头像" header-align="center" align="center">
        <template v-slot="scope">
          <el-image style="height: 50px" :src="scope.row.headUrl" :preview-src-list="[scope.row.headUrl]" fit="fit" hide-on-click-modal preview-teleported />
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" header-align="center" align="center"></el-table-column>
      <el-table-column prop="realName" label="姓名" header-align="center" align="center"></el-table-column>
      <el-table-column prop="userTag" label="教师头衔" header-align="center" align="center"></el-table-column>
      <el-table-column prop="gender" label="性别" header-align="center" align="center">
        <template v-slot="scope">
          <span>{{scope.row.gender == 0?'男':scope.row.gender == 1?'女':'保密'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" header-align="center" align="center"></el-table-column>
      <el-table-column prop="mobile" label="手机号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="jobFlag" label="在职状态" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.jobFlag == 0">在职</el-tag>
          <el-tag type="danger" v-if="scope.row.jobFlag == 1">离职</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="roleIdList" label="职位" header-align="center" align="center">
        <template v-slot="scope">
          <div v-for="(item,index) in scope.row.roleIdList" :key="index">
            <span>{{handlerRoleId(item)}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="primary" link @click="state.deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs, onMounted } from "vue";
import AddOrUpdate from "./teacher-add-or-update.vue";
import baseService from "@/service/baseService";
import { IObject } from "@/types/interface";

const view = reactive({
  getDataListURL: "/lingyao/teacherEmp/page",
  getDataListIsPage: true,
  deleteURL: "/lingyao/teacherEmp",
  deleteIsBatch: true,
  dataForm: {
    realName: "",
    roleName: ""
  }
});

const roleList = ref<IObject[]>([]);

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id);
};

onMounted(() => {
  getRoleList();
});

// 获取角色列表
const getRoleList = () => {
  baseService.get(`/lingyao/teacherEmp/getTeacherEmpRoleList`).then((res) => {
    roleList.value = res.data;
  });
};

// 处理表格中的角色列表
const handlerRoleId = (id: number) => {
  let len = roleList.value.filter((item) => item.id == id);
  return len.length > 0 ? len[0].name : "";
};
</script>
